using System;
using System.Collections.Generic;

namespace MMessaging.MessagingEx.Common
{
	public class MMessagingHandlerContext<TMESSAGETYPE>
	{
		public TMESSAGETYPE CurrentMessage { get; set; }

		public MMessagingHandlerContext()
		{
		}

		public MMessageProcessing<TMESSAGETYPE> Call(
			string comment,
			MMessageProcessing<TMESSAGETYPE>.HandlerFunc func)
		{
			return new MMessageProcessing<TMESSAGETYPE>(
				comment+"->call",
				this,
				func);
		}

		public MMessageProcessing<TMESSAGETYPE> WaitOn(
			string comment,
			Func<bool> condition)
		{
			return new MMessageProcessing<TMESSAGETYPE>(
				comment,
				this,
				context2 => ConditionMethod(
					comment + "->condition",
					condition));
		}

		private IEnumerable<MMessageProcessing<TMESSAGETYPE>> ConditionMethod(
			string comment,
			Func<bool> condition)
		{
			while(!condition())
				yield return new MMessageProcessing<TMESSAGETYPE>(comment);
		}
	}

	
}